package org.helgoboss.obr_facade

import java.net.URL
import org.osgi.service.obr.Repository

/**
 * Interface for a service treating OSGi bundle repository resources and bundles as one thing. It simplifies the lifecycle
 * of bundles by only differentiating between the two states off and on (uninstalled || installed || resolved = off, starting || stopping || active = on). installed, resolved, starting and stopping are intermediate states.
 * Switching off means turning the bundle into state uninstalled. Switching on means turning the bundle into state active.
 * Note, however, that you still have access on the bundles itself. So you are not limited in flexibility.
 */
trait ObrFacade {
    def installedRepositories: Seq[Repository]

    def installedRemoteRepositories: Seq[Repository]
        
    def installedLocalRepositories: Seq[Repository]
    
    def findInstalledRepository(repositoryUrl: URL): Option[Repository]
    
    def repositoryIsInstalled(repositoryUrl: URL) = {
        findInstalledRepository(repositoryUrl).isDefined
    }
    
    def developmentRepositoryUrl: Option[URL]
    
    def installRepository(repositoryUrl: URL)
    
    def uninstallRepository(repositoryUrl: URL)
    
    def uninstallRepository(repository: Repository) {
        installRepository(repository.getURL)
    }
    
    def installOrRefreshRepository(repositoryUrl: URL)
    
    def refreshRepository(repository: Repository) {
        installOrRefreshRepository(repository.getURL)
    }

    def findResources(filterExpression: String): Seq[Resource]

    def findResource(resourceId: ResourceId): Option[Resource]
    
    def resourceExists(resourceId: ResourceId) = {
        findResource(resourceId).isDefined
    }
    
    /**
     * Returns whether the given resource is switched on.
     */
    def resourceIsSwitchedOn(resource: ResourceId): Boolean
    
    /**
     * Switches on the given resource. If the resource is already switched on, the behavior is undefined.
     */
    def switchOnResource(resourceId: ResourceId)
    
    /**
     * Switches off the given resource. If the resource is already switched off, the behavior is undefined.
     */
    def switchOffResource(resourceId: ResourceId)
    
    /**
     * Switches the given resource off (if it was switched on) and on.
     */
    def switchOnOrRefreshResource(resourceId: ResourceId)
    
    /**
     * Switches the given resources on. This differs from executing switchOnResource several times by having a transactional character.
     * If only one of them cannot be resolved, no resource will be switched on. If one of the resources is already switched on, 
     * the behavior is undefined.
     */
    def switchOnResources(resourceIds: Iterable[ResourceId])
}   